package com.tigercel.xiangchengyun.ui.activities

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.JsonUtil
import com.tigercel.xiangchengyun.utils.PreferencesUtil
import kotlinx.android.synthetic.main.activity_bind_phone.*
import org.jetbrains.anko.toast

class BindPhoneActivity : AppCompatActivity() {
    private lateinit var authCode: String
    private var timeCount: TimeCount? = null
    private var perform: PreferencesUtil? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bind_phone)
        timeCount = TimeCount(60000, 1000)
        perform = PreferencesUtil(this)
        tv_getCode.setOnClickListener(View.OnClickListener {
            if (et_bindphone.text.isEmpty() || et_bindphone.text.length != 11 || !et_bindphone.text.startsWith("1")) {
                toast("请输入正确的手机号")
            } else {
                et_code.isFocusable = true
                et_code.requestFocus()
                timeCount!!.start()
                getCode()
            }

        })
        btn_bind.setOnClickListener { bindCode() }

    }

    internal inner class TimeCount(millisInFuture: Long, countDownInterval: Long) : CountDownTimer(millisInFuture, countDownInterval) {

        override fun onTick(millisUntilFinished: Long) {
            tv_getCode.isClickable = false
            tv_getCode.text = "(" + millisUntilFinished / 1000 + ") 秒后可重新获取"
        }

        override fun onFinish() {
            tv_getCode.text = "重新获取验证码"
            tv_getCode.isClickable = true

        }
    }

    private fun getCode() {
        OkGo.get<String>(Global.baseUrl + Global.getVerificationCode)
                .tag(this)
                .params("mobile", et_bindphone.text.trim().toString())
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        var result = response!!.body()
                        var status = JsonUtil.get_key_string("status", result)
                        var msg = JsonUtil.get_key_string("msg", result)
                        var code = JsonUtil.get_key_string("code", result)
                        var data = JsonUtil.getList("data", result)
                        if (status == "0") {
                            authCode = JsonUtil.get_key_string("authcode", data[0])
                        } else {
                            toast(getString(R.string.getAuthCodeFaile))
                        }
                    }

                })

    }

    private fun bindCode() {
        Log.e("phone",et_bindphone.text.toString())
        Log.e("code",et_code.text.toString())
        Log.e("token",perform!!.preferences["token"])
        OkGo.post<String>(Global.baseUrl + Global.bindPhone)
                .tag(this)
                .params("mobile", et_bindphone.text.toString())
                .params("authcode", et_code.text.toString())
                .params("token", perform!!.preferences["token"])
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        var result = response!!.body()
                        var status = JsonUtil.get_key_string("status", result)
                        var msg = JsonUtil.get_key_string("msg", result)
                        var code = JsonUtil.get_key_string("code", result)
                        var data = JsonUtil.getList("data", result)
                        if (status == "0") {
                            val data = Intent()
                            data.putExtra("data", et_bindphone.text.toString())
                            setResult(4, data)
                            this@BindPhoneActivity.finish()
                        }else{
                            toast("绑定失败"+msg)
                        }
                    }

                })
    }
}
